// 引入文档模型
const BookModel = require('./BookModel')

// 查找出所有的图书
findAllBooks = async () => {
    return await BookModel.find()
}

// 查找出id对应的图书
findCurrentBook = async (_id) => {
    try {
        const res = await BookModel.findById({
            _id
        })
        return {
            code: 200,
            value: res
        }
    } catch (err) {
        return {
            code: 404,
            value: '抱歉，查找不到对应的图书~'
        }
    }

}

// 向数据库中添加图书
addBook = async (book) => {
    return await BookModel.create(book)
}

// 更新数据库中的对应图书
updateBook = async (book) => {
    return await BookModel.updateOne({
        _id: book._id
    }, book)
}

// 删除数据库中对应id的图书
removeBook = async (_id) => {
    return await BookModel.deleteOne({
        _id
    })

}

// 向外暴露接口
module.exports = {
    findAllBooks,
    findCurrentBook,
    addBook,
    updateBook,
    removeBook
}